dojo.provide("lotex.report.Las");
dojo.require("dojo.lang.declare");
dojo.require("lotex.math");
dojo.require("lotex.string");
dojo.require("dojo.collections.Dictionary");

dojo.declare("lotex.report.Las", null, {
    //re = new RegExp("(=|sum|avg|x|y|[A-Z]+|\[|\]|\(|\))");
    cwReg: /(\[\d+-\d+\])/,
    fnReg: /(rmb|sum|avg|percent)/,
    regs:
        [
            /\[\d+-\d+\]/,
            /\+/,
            /\[([^,]+,)*\w+\]/
        ],
    initializer: function(s) {
        this.s = lotex.string.unescapeXml(s);
        this.words = this.cutWords(this.s);
    },

    getDict: function(){
        var regs = new dojo.collections.Dictionary();
        regs.add("wd",/\[\d+-\d+\]/);
    },

    match: function(s) {
        var r;
        var len = this.regs.length;
        for(var i=0;i<len;i++) {
            r = s.match(this.regs[i]);
            if (r != null) {
                return r;
            }
        }
        return null;
    },

    cutWords: function(s) {
        var r;
        var ret = [];
        while((r = this.match(s)) != null) {
            if(r.index>0) ret.push(s.substring(0,r.index));
            ret.push(s.substring(r.index,r.lastIndex));
            s = s.substring(r.lastIndex, s.length);
        }
        ret.push(s);

        if(ret[0]=="=") ret.shift();
        return ret;
        //b(ret);
    },

    precompile: function() {
        var w = this.words;
        for(var i=0; i<w.length; i++) {
            var s = w[i];
            if (s.match(this.fnReg) != null)
                {w[i] = "lotex.report.Functions."+s;continue;}
            if (s == "x") {w[i] = '${curRow}';continue;}
            if (s == "y") {w[i] = '${curCol}';continue;}
            if (s == "[") {w[i] = "this.getCell(";continue;}
            if (s == "]") {w[i] = ",${sheet},${scope})";continue;}
            if (s.match(/[A-Z]+/) != null) {
                if(w[i-1]=="this.getCell(" || w[i+1]=="]") {
                    w[i] = lotex.math.parseStr(s);
                } else {
                    w[i] = "this.getCell("+lotex.math.parseStr(s)+",null,${sheet},${scope})";
                }
                continue;
            }
        }
        this.compilestr = this.words.join('');
        //"File '%{0}' is not found in directory '%{1}'.","foo.html","/temp");
    },

    importVar: function(curRow,curCol,sheet,scope) {
        var s = this.compilestr;
        var d = {
                curRow:curRow,
                curCol:curCol,
                sheet:sheet,
                scope:scope
            };
        return lotex.string.sprintf(s,d);
    },

    fullSyntax: function(curRow,curCol) {
        var w = this.words;
        for(var i=0; i<w.length; i++) {
            var s = w[i];
            if (s.match(this.fnReg) != null)
                {w[i] = "lotex.report.Functions."+s;continue;}
            if (s == "x") {w[i] = curRow;continue;}
            if (s == "y") {w[i] = curCol;continue;}
            if (s == "[") {w[i] = "this.getCell(";continue;}
            if (s == "]") {w[i] = ")";continue;}
            if (s.match(/[A-Z]+/) != null) {
                if(w[i-1]=="this.getCell(" || w[i+1]=="]") {
                    w[i] = lotex.math.parseStr(s);
                } else {
                    w[i] = "this.getCell("+lotex.math.parseStr(s)+",null)";
                }
                continue;
            }
        }
    },

    toExp: function() {
        var c = this.words.join("");
        return c;
    }
});